package com.xhwl.centiotdata.handler.account.impl;

import com.alibaba.fastjson.JSONObject;
import com.xhwl.centiotdata.handler.account.AccountFactory;
import com.xhwl.centiotdata.handler.account.AccountHandler;
import com.xhwl.centiotdata.service.IOrgInfoService;
import com.xhwl.centiotdata.service.cent.IAuthAccountCentService;
import com.xhwl.centiotdata.suppliersystem.sinoiov.PedestrianSinoiovHandler;
import com.xhwl.common.dto.cent.sdata.AccountDTO;
import com.xhwl.common.enums.ProductEnum;
import com.xhwl.common.pojo.cent.auth.AuthAccountMapping;
import com.xhwl.common.pojo.cent.device.DeviceProjSupplierCategory;
import com.xhwl.common.pojo.cent.iot.OrgInfo;
import com.xhwl.common.pojo.cent.sdata.personnel.StaffDTO;
import com.xhwl.common.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * @author pengjunming
 * @description
 * @date 2022/11/9 15:16
 */
@Slf4j
@Service
public class AccountSinoiovHandler extends AccountHandler {

    @Resource
    private PedestrianSinoiovHandler pedestrianSinoiovHandler;

    /**
     * 权限关系
     */
    @Resource
    private IAuthAccountCentService iAuthPedestrianCentService;

    @Resource
    private IOrgInfoService iOrgInfoService;

    @Override
    public void add(StaffDTO dto, Integer projectId, DeviceProjSupplierCategory config) {
        log.info("员工信息" + dto);
        JSONObject properties = config.getProperties();
        List<Integer> orgIds = dto.getOrgIds();
        String host = properties.getString("host");
        String agentId = properties.getString("agentId");
        if (CollectionUtils.isNotEmpty(orgIds)) {
            Integer orgId = orgIds.get(0);
            OrgInfo orgInfo = iOrgInfoService.getOrgInfo(orgId, config.getProductId());
            if (Objects.nonNull(orgInfo) && StringUtils.isNotEmpty(orgInfo.getOrgIndexCode())) {
                agentId = orgInfo.getOrgIndexCode();
            }
        }
        log.info("使用的组织信息：{}", agentId);
        String id = pedestrianSinoiovHandler.add(dto, host, agentId, null);
        if (StringUtils.isNotEmpty(id)) {
            boolean saveAuthResult = iAuthPedestrianCentService.save(projectId, dto.getAccountId(), id, null, config.getProductId());
            log.info("账号关联关系保存结果 : {}", saveAuthResult);
        }
    }

    @Override
    public void delete(AccountDTO dto, Integer projectId, DeviceProjSupplierCategory config) {
        if (null != config) {
            Map<String, Object> params = new HashMap<>();
            JSONObject properties = config.getProperties();
            //查询关联表
            AuthAccountMapping authAccount = iAuthPedestrianCentService.query(projectId, dto.getId(), config.getProductId());
            if (null != authAccount) {
                String host = properties.getString("host");
                String agentId = properties.getString("agentId");
                params.put("personIds", Collections.singletonList(authAccount.getOpenId()));
                pedestrianSinoiovHandler.delete(host, agentId, dto.getPhone(), authAccount.getOpenId());
                iAuthPedestrianCentService.deleteAccountMapping(projectId, dto.getId(), authAccount.getOpenId(), config.getProductId());
            }
        }
    }

    @Override
    public void update(StaffDTO dto, Integer projectId, DeviceProjSupplierCategory config) {
        //查询关联表
        AuthAccountMapping authAccount = iAuthPedestrianCentService.query(projectId, dto.getAccountId(), config.getProductId());
        //没查到关联关系，则新增
        if (null == authAccount) {
            add(dto, projectId, config);
        } else {
            handleUpdate(dto, config, authAccount);
        }
    }

    private void handleUpdate(StaffDTO dto, DeviceProjSupplierCategory config, AuthAccountMapping authAccount) {
        JSONObject properties = config.getProperties();
        List<Integer> orgIds = dto.getOrgIds();
        String host = properties.getString("host");
        String agentId = properties.getString("agentId");
        if (CollectionUtils.isNotEmpty(orgIds)) {
            Integer orgId = orgIds.get(0);
            OrgInfo orgInfo = iOrgInfoService.getOrgInfo(orgId, config.getProductId());
            if (Objects.nonNull(orgInfo) && StringUtils.isNotEmpty(orgInfo.getOrgIndexCode())) {
                agentId = orgInfo.getOrgIndexCode();
            }
        }
        pedestrianSinoiovHandler.add(dto, host, agentId, authAccount.getOpenId());
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        AccountFactory.register(ProductEnum.SINOIOV_PEDE.getId(), this);
        log.info("中交兴路账号管理注册成功");
    }


}
